package cn.zcscloud.service.impl;

import cn.zcscloud.mapper.UserMapper;
import cn.zcscloud.pojo.User;
import cn.zcscloud.service.UserService;
import cn.zcscloud.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserServiceImpl implements UserService {
    public User login(String username, String password) {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //创建sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取sql执行对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //执行sql语句
        User user = userMapper.login(username, password);
        return user;
    }

    public void register(User user) {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //创建sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取sql执行对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //执行sql语句
        userMapper.register(user);
        sqlSession.commit();
    }

    public User findUserByName(String name) {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //创建sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取sql执行对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //执行sql语句
        User user = userMapper.findUserByName(name);
        return user;
    }
}
